package cn.zbzcode.mq._06springboot_mq;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitmqConfig {
    //邮件消费者的队列
    public static final String QUEUE_INFORM_EMAIL = "queue_inform_email";
    //短信消费者的队列
    public static final String QUEUE_INFORM_SMS = "queue_inform_sms";
    //topic类型的交换机
    public static final String EXCHANGE_TOPICS_INFORM = "exchange_topics_inform";

    /**
     * 交换机配置
     * ExchangeBuilder提供了fanout、direct、topic、header交换机类型的配置
     *
     * @return the exchange
     */
    @Bean(EXCHANGE_TOPICS_INFORM)
    public Exchange EXCHANGE_TOPICS_INFORM(){
        //durable(true)持久化，消息队列重启后交换机仍然存在
        return ExchangeBuilder.topicExchange(EXCHANGE_TOPICS_INFORM).durable(true).build();
    }
    //声明队列email
    @Bean(QUEUE_INFORM_EMAIL)
    public Queue QUEUE_INFORM_EMAIL(){
        //3.声明队列:队列名字 ； 持久 ;独占链接 ；不用即删除 ； 参数
        Queue queue = new Queue(QUEUE_INFORM_EMAIL, true,false,false,null);
        return queue;
    }
    //声明队列sms
    @Bean(QUEUE_INFORM_SMS)
    public Queue QUEUE_INFORM_SMS(){
        //3.声明队列:队列名字 ； 持久 ;独占链接 ；不用即删除 ； 参数
        Queue queue = new Queue(QUEUE_INFORM_SMS, true,false,false,null);
        return queue;
    }
    /**
     * channel.queueBind(INFORM_QUEUE_SMS,"inform_exchange_topic","inform.#.sms.#");
     * 绑定队列到交换机 .
     * @Qualifier(QUEUE_INFORM_EMAIL)：在候选Bean数目不为1时应该加@Qualifier注释，表明哪一个bean才是我们需要的
     *
     * @param email_queue    the queue
     * @param exchange the exchange
     * @return the binding
     */
    @Bean
    public Binding BINDING_QUEUE_INFORM_EMAIL(@Qualifier(QUEUE_INFORM_EMAIL)Queue email_queue,@Qualifier(EXCHANGE_TOPICS_INFORM) Exchange exchange){
        return BindingBuilder.bind(email_queue).to(exchange).with("email.#").noargs();
    }

    @Bean
    public Binding BINDING_QUEUE_INFORM_SMS(@Qualifier(QUEUE_INFORM_SMS)Queue sms_queue,@Qualifier(EXCHANGE_TOPICS_INFORM) Exchange exchange){
        return BindingBuilder.bind(sms_queue).to(exchange).with("sms.#").noargs();
    }
}